package com.zhou.imaotai.security.handle;


import com.alibaba.fastjson2.JSON;
import com.zhou.imaotai.model.LoginUser;
import com.zhou.imaotai.entity.R;
import com.zhou.imaotai.service.web.TokenService;
import com.zhou.imaotai.utils.ServletUtils;
import com.zhou.imaotai.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lcz
 * @desc 自定义退出处理类 返回成功
 * @mail lcz_0130@163.com
 * @date 2024/03/26
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    @Autowired
    private TokenService tokenService;

    /**
     * 退出处理
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
        }
        ServletUtils.renderString(response, JSON.toJSONString(R.error(200, "退出成功")));
    }
}
